define(function(require, exports, module) {

	"use strict";
	require("mui");
	require("jquery");
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	var template = require("template");
	var dataCached = [];
	var selectGoodsIds = [];
	//首页客户列表返回
	var commonBack = require("commonBack");
	mui("body").on("tap", ".client-list-back", function() {
		commonBack.secondBack();
	});
	//钉钉返回
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	var dingTalkNav = require('dd_config');
	dingTalkNav.LeftNavBar(commonBack.secondBack);
	//ad
	dingTalkNav.leftAndroid(commonBack.secondBack);
	dingTalkNav.RightNavBar(false, "更多", "default");
	mui.init({
		swipeBack: true //启用右滑关闭功能
	});

	mui('body').on('tap', '#goodsList li>a', function() {

		var goodsId = $(this).attr("data-id");
		var number = $("#number_" + goodsId).html();

		if(parseFloat(goodsId) > 0 && number !== '') {
			sessionStorage.setItem('data-production', JSON.stringify(dataCached["id-" + goodsId]));
			window.location.href = "simulateProductionInfo.html?dd_nav_bgcolor=FFFF943E";
		}
	});

	var checkedGoodsStr = sessionStorage.getItem('checkedGoods');
	//sessionStorage.removeItem("checkedGoods");

	if(checkedGoodsStr !== null) {
		var checkedGoods = JSON.parse(checkedGoodsStr);

		var data1 = {};
		data1.items = checkedGoods;

		var html = template('bindGoodsList', data1);

		$('#goodsList').html(html);

		for(var i = 0; i < data1.items.length; i++) {
			var goodsId = data1.items[i].id;
			selectGoodsIds[i] = goodsId;

			if(data1.items[i].dataCached) {
				dataCached["id-" + goodsId] = data1.items[i].dataCached;
				$("#link_" + goodsId).attr("data-id", goodsId);
				$("#number_" + goodsId).html(data1.items[i].dataCached.productionNumber);
			}
		}
	}
	mui("body").on("tap", "#back", function() {

		sessionStorage.removeItem("checkedGoods");

	});
	mui('body').on('tap', '#start', function() {

		var option = {};
		var dataModel = {
			"goodsIds": selectGoodsIds
		};

		if(dataModel.goodsIds.length === 0) {

			mui.alert('<ul class="mui-row pop-tabmain"><li class="mui-col-xs-4 mui-col-sm-4"style="text-align: center;"><a class="yuanico"><span>!</span></a></li><li class="mui-col-xs-8 mui-col-sm-8" style="text-align: left;line-height: 24px;">请先选择货品</li></ul>', ' ', function() {}, 'div');

			return;
		}

		var checkedGoods = JSON.parse(checkedGoodsStr);

		Start("bindGoodsList", "#goodsList");

		function Start(templateId, mainId) {

			abp.services.app.infoBom.getAppProductionList(dataModel, option).
			done(function(data) {

				var data1 = {};
				data1.items = data;
				bindGoodsList
				for(var i = 0; i < data1.items.length; i++) {
					var goodsId = data1.items[i].goodsId;
					$("#link_" + goodsId).attr("data-id", goodsId);
					$("#number_" + goodsId).html(data1.items[i].productionNumber);

					dataCached["id-" + goodsId] = data1.items[i];

					for(var j = 0; j < checkedGoods.length; j++) {
						if(checkedGoods[j].id == goodsId) {
							checkedGoods[j].dataCached = dataCached["id-" + goodsId];
							break;
						}
					}
				}

				checkedGoods = JSON.stringify(checkedGoods);
				sessionStorage.checkedGoods = checkedGoods;

			}).fail(function(data) {

			});

		}

	});

	// 监听tap事件，解决 a标签 不能跳转页面问题
	mui('body').on('tap', 'a', function() {
		if(this.href) {
			document.location.href = this.href + "&dd_nav_bgcolor=FFFF943E";
		}
	});

	$(function() {

	});

});